package com.avalon.shop.controller;


import com.avalon.core.model.RecordRow;
import com.avalon.shop.service.PetAdoptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/adopt")
public class PetAdoptController {
    @Autowired
    private PetAdoptService petAdoptService;

    @PostMapping("/publish/pet")
    public void publishPetAdopt(@RequestBody Map<String, Object> param) {
        Integer publishId = 0;
        if (param.containsKey("publishId")) {
            publishId = (Integer) param.get("publishId");
        }
        Integer petId = 0;
        if (param.containsKey("petId")) {
            petId = (Integer) param.get("petId");
        }

        String selfDescription = "";
        if (param.containsKey("selfDescription")) {
            selfDescription = (String) param.get("selfDescription");
        }

        String avatar = "";
        if (param.containsKey("avatar")) {
            avatar = (String) param.get("avatar");
        }

        String video = "";
        if (param.containsKey("video")) {
            video = (String) param.get("video");
        }

        String openId = "";
        if (param.containsKey("openId")) {
            openId = (String) param.get("openId");
        }

        petAdoptService.publishPetAdopt(openId, petId, selfDescription, publishId, avatar, video);
    }

    @PostMapping("get/pet/list")
    public List<Map> getAdoptList(@RequestBody Map<String, Object> param) {
        Integer pageNum = 1;
        if (param.containsKey("pageNum")) {
            pageNum = Integer.parseInt(param.get("pageNum").toString());
        }
        Integer pageSize = 10;
        if (param.containsKey("pageSize")) {
            pageSize = Integer.parseInt(param.get("pageSize").toString());
        }
        String searchName = "";
        if (param.containsKey("searchName")) {
            searchName = param.get("searchName").toString();
        }

        String checkState = "";
        if (param.containsKey("checkState")) {
            checkState = param.get("checkState").toString();
        }

        Integer userId = null;
        if (param.containsKey("userId")) {
            userId = Integer.parseInt(param.get("userId").toString());
        }
        return petAdoptService.getAdoptList(pageNum, pageSize, searchName, checkState, userId);
    }

    @PostMapping("/get/adopt/detail")
    public Map getAdoptDetail(@RequestBody Map<String, Object> param) {
        int id = 0;
        if (param.containsKey("id")) {
            id = Integer.parseInt(param.get("id").toString());
        }
        return petAdoptService.getAdoptDetail(id);
    }

    @PostMapping("/get/message")
    public Map getAdoptMessageDetail(@RequestBody Map<String, Object> param) {
        Integer id = 0;
        if (param.containsKey("id")) {
            id = Integer.parseInt(param.get("id").toString());
        }
        return petAdoptService.getAdoptMessage(id);
    }

    @PostMapping("/insert/message")
    public RecordRow insertAdoptMessage(@RequestBody Map<String, Object> param) {
        Integer petAdoptId = 0;
        if (param.containsKey("petAdoptId")) {
            petAdoptId = Integer.parseInt(param.get("petAdoptId").toString());
        }
        Integer petUserId = 0;
        if (param.containsKey("petUserId")) {
            petUserId = Integer.parseInt(param.get("petUserId").toString());
        }
        String message = "";
        if (param.containsKey("message")) {
            message = param.get("message").toString();
        }
        return petAdoptService.insertAdoptMessage(petAdoptId, petUserId, message);
    }

    @PostMapping("/get/message/page")
    public List<Map> getAdoptMessage(@RequestBody Map<String, Object> param) {
        Integer pageNum = 1;
        if (param.containsKey("pageNum")) {
            pageNum = Integer.parseInt(param.get("pageNum").toString());
        }
        Integer pageSize = 10;
        if (param.containsKey("pageSize")) {
            pageSize = Integer.parseInt(param.get("pageSize").toString());
        }
        Integer petAdoptId = 0;
        if (param.containsKey("petAdoptId")) {
            petAdoptId = Integer.parseInt(param.get("petAdoptId").toString());
        }
        return petAdoptService.getAdoptMessage(pageNum, pageSize, petAdoptId);
    }

    @PostMapping("/message/count")
    public Map getAdoptMessageCount(@RequestBody Map<String, Object> param) {
        Integer petAdoptId = 0;
        if (param.containsKey("petAdoptId")) {
            petAdoptId = Integer.parseInt(param.get("petAdoptId").toString());
        }
        int count = petAdoptService.getAdoptMessageCount(petAdoptId);
        Map<String, Object> result = new HashMap<>();
        result.put("count", count);
        return result;
    }

    @PostMapping("/delete/pet/adopt")
    public void deletePetAdopt(@RequestBody Map<String, Object> param) {
        Integer petAdoptId = 0;
        if (param.containsKey("petAdoptId")) {
            petAdoptId = Integer.parseInt(param.get("petAdoptId").toString());
        }
        petAdoptService.deleteModel(petAdoptId);
    }

    @PostMapping("/success/pet/adopt")
    public void successPetAdopt(@RequestBody Map<String, Object> param) {
        Integer petAdoptId = 0;
        if (param.containsKey("petAdoptId")) {
            petAdoptId = Integer.parseInt(param.get("petAdoptId").toString());
        }
        Map<String, Object> body = new HashMap<>();
        body.put("id", petAdoptId);
        body.put("state", "success");
        petAdoptService.updateModel(body);
    }
}
